package com.jboost.common.api.vo;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jboost.common.constant.CommonConstant;
import com.jboost.common.constant.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    @ApiModelProperty(value = "成功标志", notes = "正常为true，业务逻辑错误为false")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 1; //默认为1

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T result;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    @Deprecated
    public static Result<Object> ok() {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage("成功");
        return r;
    }

    @Deprecated
    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        return r;
    }

    @Deprecated
    public static Result<Object> ok(Object data) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage("成功");

        return r;
    }





    public static <T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        if (data instanceof String) {
            JSONObject result = new JSONObject();
            result.put("message", data);
            r.setResult((T) result);
        } else {
            r.setResult(data);
        }
        return r;



    }




    public static <T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static Result<Object> OK(ResultCode resultCode) {
        Result<Object> r = new Result<Object>();
        r.setCode(resultCode.getCode());
        r.setMessage(resultCode.getMsg());
        r.setSuccess(true);
        return r;
    }

    public static <T> Result<T> ERROR(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        if (data instanceof String) {
            JSONObject result = new JSONObject();
            result.put("message", data);
            r.setResult((T) result);
        } else {
            r.setResult(data);
        }
        return r;
    }
    public static <T> Result<T> ERROR(ResultCode resultCode,T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(resultCode.getCode());
        r.setMessage(resultCode.getMsg());
        if(null != data){
            if (data instanceof String) {
                JSONObject result = new JSONObject();
                result.put("message", data);
                r.setResult((T) result);
            } else {
                r.setResult(data);
            }
        }
        return r;
    }

    public static <T> Result<T> ERROR(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    /**
     * 业务参数验证异常错误
     *
     * @param msg
     * @return
     */
    public static Result<Object> paramError(String msg) {
        return error(ResultCode.PARAMETER_VERIFY_FAIL.getCode(), msg);
    }

    public static Result<Object> error(ResultCode resultCode) {
        Result<Object> r = new Result<Object>();
        r.setCode(resultCode.getCode());
        r.setMessage(resultCode.getMsg());
        r.setSuccess(false);
        return r;
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static <T> Result<T> errorCode(int code, String msg, T data) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        r.setResult(data);
        return r;
    }

    public Result<T> errorCode(int code, String msg) {
        this.code = code;
        this.message = msg;
        this.success = false;
        return this;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }

    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
    }

    @JsonIgnore
    private String onlTable;

}
